/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "NativeImageTest.h"
#include "napi/native_api.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl32.h>
#include <bits/alltypes.h>
#include <cstring>
#include <native_buffer/native_buffer.h>
#include <native_image/native_image.h>
#include <native_window/external_window.h>
#include <sys/mman.h>
#include <chrono>
#include <iostream>
#include <map>
#include <native_image/graphic_error_code.h>

napi_property_descriptor desc[] = {
    {"oHNativeImageAcquireNativeWindowNullptr", nullptr, OHNativeImageAcquireNativeWindowNullptr, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowNormal", nullptr, OHNativeImageAcquireNativeWindowNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageAttachContextNullptr", nullptr, OHNativeImageAttachContextNullptr, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageDetachContextNullptr", nullptr, OHNativeImageDetachContextNullptr, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageAttachContextNormal", nullptr, OHNativeImageAttachContextNormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageDetachContextNormal", nullptr, OHNativeImageDetachContextNormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageGetSurfaceIdNullptr", nullptr, OHNativeImageGetSurfaceIdNullptr, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeGetSurfaceIdNormal", nullptr, OHNativeGetSurfaceIdNormal, nullptr, nullptr, nullptr, napi_default,
        nullptr},
    {"oHNativeImageUpdateSurfaceImageNullptr", nullptr, OHNativeImageUpdateSurfaceImageNullptr, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetTimestampNullptr", nullptr, OHNativeImageGetTimestampNullptr, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageGetTransformMatrixNullptr", nullptr, OHNativeImageGetTransformMatrixNullptr, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetTransformMatrixV2Nullptr", nullptr, OHNativeImageGetTransformMatrixV2Nullptr, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageUpdateSurfaceImageNormal", nullptr, OHNativeImageUpdateSurfaceImageNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetTimestampNormal", nullptr, OHNativeImageGetTimestampNormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageGetTransformMatrixNormal", nullptr, OHNativeImageGetTransformMatrixNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetTransformMatrixV2Normal", nullptr, OHNativeImageGetTransformMatrixV2Normal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageSetOnFrameAvailableListenerNullptr", nullptr, OHNativeImageSetOnFrameAvailableListenerNullptr,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageUnSetOnFrameAvailableListenerNullptr", nullptr,
        OHNativeImageUnSetOnFrameAvailableListenerNullptr, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageSetOnFrameAvailableListenerNormal", nullptr, OHNativeImageSetOnFrameAvailableListenerNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageUnsetOnFrameAvailableListenerNormal", nullptr, OHNativeImageUnsetOnFrameAvailableListenerNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
        
    {"oHNativeImageCreate", nullptr, OHNativeImageCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindow", nullptr, OHNativeImageAcquireNativeWindow, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowAbnormal", nullptr, OHNativeImageAcquireNativeWindowAbnormal, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAttachContext", nullptr, OHNativeImageAttachContext, nullptr, nullptr, nullptr, napi_default,
        nullptr},
    {"oHNativeImageAttachContextAbnormal", nullptr, OHNativeImageAttachContextAbnormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageDetachContext", nullptr, OHNativeImageDetachContext, nullptr, nullptr, nullptr, napi_default,
        nullptr},
    {"oHNativeImageDetachContextAbnormal", nullptr, OHNativeImageDetachContextAbnormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageUpdateSurfaceImage", nullptr, OHNativeImageUpdateSurfaceImage, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageUpdateSurfaceImageBoundary", nullptr, OHNativeImageUpdateSurfaceImageBoundary, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageUpdateSurfaceImageAbnormal", nullptr, OHNativeImageUpdateSurfaceImageAbnormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetTimestamp", nullptr, OHNativeImageGetTimestamp, nullptr, nullptr, nullptr, napi_default,
        nullptr},
    {"oHNativeImageGetTransformMatrix", nullptr, OHNativeImageGetTransformMatrix, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageGetTransformMatrixAbnormal", nullptr, OHNativeImageGetTransformMatrixAbnormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageDestroy", nullptr, OHNativeImageDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateNormal", nullptr, OHNativeImageCreateNormal, nullptr, nullptr, nullptr, napi_default,
        nullptr},
    {"oHNativeImageCreateAbnormal", nullptr, OHNativeImageCreateAbnormal, nullptr, nullptr, nullptr, napi_default,
        nullptr},
    {"oHNativeImageDestroy1", nullptr, OHNativeImageDestroy1, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateMuch", nullptr, OHNativeImageCreateMuch, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeWindowDropBufferModeSetTrueNormal2", nullptr, OHNativeWindowDropBufferModeSetTrueNormal2,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeWindowDropBufferModeSetAbNormal", nullptr, OHNativeWindowDropBufferModeSetAbNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseNativeWindowBufferNormal", nullptr, OHNativeImageReleaseNativeWindowBufferNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseNativeWindowBufferAbnormal01", nullptr, OHNativeImageReleaseNativeWindowBufferAbnormal01,
        nullptr, nullptr, nullptr, napi_default, nullptr},
        
    {"oHNativeImageGetBufferMatrixNormal", nullptr, OHNativeImageGetBufferMatrixNormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageGetBufferMatrixNormal01", nullptr, OHNativeImageGetBufferMatrixNormal01, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetBufferMatrixAbNormal", nullptr, OHNativeImageGetBufferMatrixAbNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHNativeImageGetBufferMatrixCall", nullptr, OHNativeImageGetBufferMatrixCall, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferNormal", nullptr, OHNativeImageAcquireNativeWindowBufferNormal, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferCall", nullptr, OHNativeImageAcquireNativeWindowBufferCall, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferAbNormal01", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal01,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferAbNormal02", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal02,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferAbNormal03", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal03,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferAbNormal04", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal04,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireNativeWindowBufferAbNormal05", nullptr, OHNativeImageAcquireNativeWindowBufferAbNormal05,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseNativeWindowBufferAbnormal", nullptr, OHNativeImageReleaseNativeWindowBufferAbnormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultUsageNormal", nullptr, OHConsumerSurfaceSetDefaultUsageNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultUsageCall", nullptr, OHConsumerSurfaceSetDefaultUsageCall, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultUsageAbNormal", nullptr, OHConsumerSurfaceSetDefaultUsageAbNormal, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultSizeNormal", nullptr, OHConsumerSurfaceSetDefaultSizeNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultSizeCall", nullptr, OHConsumerSurfaceSetDefaultSizeCall, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultSizeAbNormal", nullptr, OHConsumerSurfaceSetDefaultSizeAbNormal, nullptr, nullptr,
        nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceSetDefaultSizeAbNormal01", nullptr, OHConsumerSurfaceSetDefaultSizeAbNormal01, nullptr,
        nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceCreateAbNormal", nullptr, oHConsumerSurfaceCreateAbNormal, nullptr, nullptr, nullptr,
        napi_default, nullptr},
    {"OHConsumerSurfaceSetDefaultUsageSizeNormal", nullptr, OHConsumerSurfaceSetDefaultUsageSizeNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeWindowDropBufferModeSetTrueNormal", nullptr, OHNativeWindowDropBufferModeSetTrueNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
        
    {"oHNativeImageCreateWithSingleBufferModeUsingTextImageNormal", nullptr,
        OHNativeImageCreateWithSingleBufferModeUsingTextImageNormal, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateWithSingleBufferModeUsingTextImageNull", nullptr,
        OHNativeImageCreateWithSingleBufferModeUsingTextImageNull, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateWithSingleBufferModeUsingTextImageAbnormal", nullptr,
        OHNativeImageCreateWithSingleBufferModeUsingTextImageAbnormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateWithSingleBufferModeUsingTextImageMultipleCalls", nullptr,
        OHNativeImageCreateWithSingleBufferModeUsingTextImageMultipleCalls,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceCreateWithSingleBufferModeUnusingTextImageNormal", nullptr,
        OHConsumerSurfaceCreateWithSingleBufferModeUnusingTextImageNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceCreateWithSingleBufferModeUnusingTextImageAbnormal", nullptr,
        OHConsumerSurfaceCreateWithSingleBufferModeUnusingTextImageAbnormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceCreateWithSingleBufferModeUnusingTextImageMultipleCalls", nullptr,
        OHConsumerSurfaceCreateWithSingleBufferModeUnusingTextImageMultipleCalls,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseTextImageNormal", nullptr, OHNativeImageReleaseTextImageNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseTextImageNull", nullptr, OHNativeImageReleaseTextImageNull,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseTextImageMultipleCalls", nullptr, OHNativeImageReleaseTextImageMultipleCalls,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageReleaseTextImageDestroyImage", nullptr, OHNativeImageReleaseTextImageDestroyImage,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageGetColorSpaceNormal", nullptr, OHNativeImageGetColorSpaceNormal,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageGetColorSpaceNull", nullptr, OHNativeImageGetColorSpaceNull,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageGetColorSpaceMultipleCalls", nullptr, OHNativeImageGetColorSpaceMultipleCalls,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageGetColorSpaceDestoryImage", nullptr, OHNativeImageGetColorSpaceDestoryImage,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateWithSingleBufferModeTest1", nullptr, OHNativeImageCreateWithSingleBufferModeTest1,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateWithSingleBufferModeTest2", nullptr, OHNativeImageCreateWithSingleBufferModeTest2,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageCreateWithSingleBufferModeTest3", nullptr, OHNativeImageCreateWithSingleBufferModeTest3,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHConsumerSurfaceCreateWithSingleBufferModeTest", nullptr, OHConsumerSurfaceCreateWithSingleBufferModeTest,
        nullptr, nullptr, nullptr, napi_default, nullptr},
    
    {"oHNativeImageAcquireLatestNativeWindowBufferAbNormal", nullptr,
        OHNativeImageAcquireLatestNativeWindowBufferAbNormal, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireLatestNativeWindowBufferAbNormal01", nullptr,
        OHNativeImageAcquireLatestNativeWindowBufferAbNormal01, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireLatestNativeWindowBufferAbNormal02", nullptr,
        OHNativeImageAcquireLatestNativeWindowBufferAbNormal02, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireLatestNativeWindowBufferAbNormal03", nullptr,
        OHNativeImageAcquireLatestNativeWindowBufferAbNormal03, nullptr, nullptr, nullptr, napi_default, nullptr},
    {"oHNativeImageAcquireLatestNativeWindowBufferCall", nullptr,
        OHNativeImageAcquireLatestNativeWindowBufferCall, nullptr, nullptr, nullptr, napi_default, nullptr},
};

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "nativeimage",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); };
